package com.qf.fmall.shopcart.controller;

import com.qf.fmall.shopcart.entity.ShoppingCart;
import com.qf.fmall.shopcart.service.IShoppingCartService;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongjl
 * @since 2024-01-18
 */
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    @GetMapping("/listbycids")
    public ResultVo listbycids(@RequestParam("cids") List<String> cids){
        List<ShoppingCart> data =  shoppingCartService.listbycids(cids);
        return new ResultVo(10000, "success",data);
    }

    @PutMapping("/update/{cid}/{num}")
    public ResultVo update(@PathVariable("cid") Integer cid,@PathVariable("num") Integer num){
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setCartId(cid);
        shoppingCart.setCartNum(num+"");
        shoppingCartService.updateById(shoppingCart);
        return new ResultVo(10000, "success",null);
    }

    @GetMapping("/list")
    public ResultVo list(Integer userId){

        List<ShoppingCart> data  = shoppingCartService.listCart(userId);
        return new ResultVo(10000, "success",data);
    }


    @PostMapping("/add")
    public ResultVo add(@RequestBody ShoppingCart shoppingCart){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        shoppingCart.setCartTime(time);
        shoppingCartService.save(shoppingCart);
        return new ResultVo(10000, "success",null);
    }

}
